package com.homegoods.orderinfodetail.item;

import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hedgehog.ratingbar.RatingBar;
import com.homegoods.orderinfodetail.BR;
import com.homegoods.orderinfodetail.R;
import com.homegoods.orderinfodetail.adapter.CommentImageAdapter;
import com.homegoods.orderinfodetail.model.CommentModel;
import com.homegoods.orderinfodetail.model.PicViewInfo;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.BaseItem;
import com.kedll.library.widget.NoScrollGridView;
import com.kedll.library.widget.SecretTextView;
import com.previewlibrary.GPreviewBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author QiQi
 * @ClassName: CommentItem
 * @date 2018/9/21
 * @description
 */
public class CommentItem extends BaseItem<CommentModel> {
    public CommentItem(CommentModel data, MultiTypeAdapter adapter) {
        super(data);
        setOnClickListener(v -> {
            if (getHaveGood() == 1) {
                return;
            }

            data.setGoodNum(String.valueOf(Integer.valueOf(data.getGoodNum()) + 1));
            notifyPropertyChanged(BR.goodNum);
            data.setHaveGood(1);
            notifyPropertyChanged(BR.haveGood);
        });
    }

    @Bindable
    public int getHaveGood() {
        return getData().getHaveGood();
    }

    @BindingAdapter("android:text")
    public static void setText(SecretTextView textView, String text) {
        textView.setText(text);
    }

    @BindingAdapter("android:textColor")
    public static void setTextColor(TextView textView, int haveGood) {
        if (haveGood == 0) {
            textView.setTextColor(Color.parseColor("#5C5C5C"));
        } else {
            textView.setTextColor(Color.parseColor("#a541c9"));
        }
    }

    @BindingAdapter("haveGood")
    public static void setImage(ImageView image, int haveGood) {
        image.getDrawable().setLevel(haveGood);
    }

    @BindingAdapter(value = {"enteries"})
    public static void setAdapter(NoScrollGridView gridView, List<String> imgs) {
        /*此处设置图片放大  预览图片*/
        CommentImageAdapter imageAdapter = new CommentImageAdapter(gridView.getContext());
        imageAdapter.addAll(imgs);
        gridView.setAdapter(imageAdapter);
        gridView.setOnItemClickListener((parent, view, position, id) -> {
            List<PicViewInfo> mThumbViewInfoList = new ArrayList<>();
            for (String url : imgs) {
                PicViewInfo picViewInfo = new PicViewInfo();
                picViewInfo.setImgUrl(url);
                mThumbViewInfoList.add(picViewInfo);
            }
            computeBoundsBackward(gridView.getFirstVisiblePosition(), gridView, mThumbViewInfoList);
            GPreviewBuilder.from(gridView.getContext())
                    .setData(mThumbViewInfoList)
                    .setCurrentIndex(position)
                    .setType(GPreviewBuilder.IndicatorType.Number)
                    .start();
        });
    }

    private static void computeBoundsBackward(int firstCompletelyVisiblePos, AbsListView listView, List<PicViewInfo> mThumbViewInfoList) {
        for (int i = firstCompletelyVisiblePos; i < mThumbViewInfoList.size(); i++) {
            View itemView = listView.getChildAt(i - firstCompletelyVisiblePos);
            Rect bounds = new Rect();
            if (itemView != null) {
                itemView.getGlobalVisibleRect(bounds);
            }
            mThumbViewInfoList.get(i).setBound(bounds);
        }
    }

    @BindingAdapter(value = {"star"})
    public static void setStar(RatingBar ratingBar, int score) {
        ratingBar.setStar(score);
    }

    public String getHeadUrl() {
        return getData().getHeadUrl();
    }

    public String getPhoneNum() {
        return getData().getPhoneNum();
    }

    public int getScore() {
        return getData().getScore();
    }

    public String getDate() {
        return getData().getDate();
    }

    public String getColor() {
        return getData().getColor();
    }

    public String getSize() {
        return getData().getSize();
    }

    public String getComment() {
        return getData().getComment();
    }

    public List<String> getImgs() {
        return getData().getImgs();
    }

    @Bindable
    public String getGoodNum() {
        return getData().getGoodNum();
    }

    @Override
    public int getLayout() {
        return R.layout.info_item_comment;
    }

}
